package com.example.jiangkt.ergongyi.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.jiangkt.ergongyi.R;

import java.util.List;
import java.util.Map;

/**
 * Created by jiangkt on 2018/3/12.
 */

public class PlayerListAdapter extends BaseAdapter {
    private Context context;
    private List<Map<String,String>> playerList;

    public PlayerListAdapter(Context context, List<Map<String,String>> playerList){
        this.context = context;
        this.playerList = playerList;
    }

    @Override
    public int getCount() {
        return playerList.size();
    }

    @Override
    public Object getItem(int i) {
        return playerList.get(i).get("playerName");
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    /**
     * 再次优化后的ListView
     * @param i
     * @param view
     * @param viewGroup
     * @return
     */
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if(view==null) {
//            view = View.inflate(context, R.layout.room_info, null);
            //和上面那个方式比，不知道那种方式较好
            view = LayoutInflater.from(context).inflate(R.layout.other_player,viewGroup,false);
            holder = new ViewHolder();
            holder.playerName = view.findViewById(R.id.player_name);
            holder.playerChip = view.findViewById(R.id.player_chip);
            view.setTag(holder);//将Holder存储到convertView中
        }else {
            holder = (ViewHolder) view.getTag();
        }

        holder.playerName.setTextColor(context.getResources().getColor(R.color.colorWhite));
        holder.playerChip.setTextColor(context.getResources().getColor(R.color.colorWhite));
        holder.playerName.setText(playerList.get(i).get("playerName")+"的结算筹码：");
        holder.playerChip.setText(playerList.get(i).get("playerChip"));
        return view;
    }

    static class ViewHolder{
        TextView playerName;
        TextView playerChip;
    }
}
